<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class VendorAPI extends CI_Controller {

	public function index()
	{
		$this->load->helper('url');
		$this->load->view('v_vendorAPI');
	}
	
	public function getBills()
	{
		//Expect to be sent userid, vendorname, 
		$userid = $this->input->post('userid', TRUE);
		$vendorid = $this->input->post('vendorid', TRUE);
		
		$messageresponse;
		
		//Validate user
		if($vendorid == "CSULB")
		{
			if($userid == 1)
			{
				$messageresponse->status = "Success";
				$messageresponse->code = 0;
				
				//Create array for bill(?)
				//$messageresponse->name = "Bob";
				//$messageresponse->vendor = "CSULB";
				//$messageresponse->duedate = "4-15-12";
				//$messageresponse->description = "Cell Phone Payment";
				//$messageresponse->state = "Unpaid";
				
				$bills = Array();
				
				$bill->id = "0";
				$bill->name = "Bob Jones";
				$bill->vendor = "CSULB";
				$bill->duedate = "4-15-12";
				$bill->description = "Student Fee";
				$bill->state = "Unpaid";
				
				$bill2->id = "1";
				$bill2->name = "Bob Jones";
				$bill2->vendor = "CSULB";
				$bill2->duedate = "4-20-12";
				$bill2->description = "Parking Fee";
				$bill2->state = "Unpaid";
				
				$bill3->id = "2";
				$bill3->name = "Bob Jones";
				$bill3->vendor = "CSULB";
				$bill3->duedate = "4-21-12";
				$bill3->description = "Book Fee";
				$bill3->state = "Unpaid";
				
				$bill4->id = "3";
				$bill4->name = "Bob Jones";
				$bill4->vendor = "CSULB";
				$bill4->duedate = "4-1-12";
				$bill4->description = "Convenence Fee";
				$bill4->state = "Unpaid";
				
				array_push($bills, $bill);
				array_push($bills, $bill2);
				array_push($bills, $bill3);
				array_push($bills, $bill4);
				
				$messageresponse->bills = $bills;
			}
			else if($userid == 2)
			{
				$messageresponse->status = "Success";
				$messageresponse->code = 0;
				
				$bills = Array();
				
				$bill->id = "4";
				$bill->name = "Bob Jones";
				$bill->vendor = "CSULB";
				$bill->duedate = "4-15-12";
				$bill->description = "Student Fee";
				$bill->state = "Unpaid";
				
				array_push($bills, $bill);
				
				$messageresponse->bills = $bills;
			}
			else if($userid == 3)
			{
				$messageresponse->status = "Success";
				$messageresponse->code = 0;
				
				$bills = Array();
				
				$bill->id = "5";
				$bill->name = "Bob Jones";
				$bill->vendor = "CSULB";
				$bill->duedate = "4-15-12";
				$bill->description = "Student Fee";
				$bill->state = "Unpaid";
				
				array_push($bills, $bill);
				
				$messageresponse->bills = $bills;
			}
			else if($userid == 4)
			{
				$messageresponse->status = "Success";
				$messageresponse->code = 0;
				
				$bills = Array();
				
				$bill->id = "6";
				$bill->name = "Bob Jones";
				$bill->vendor = "CSULB";
				$bill->duedate = "4-15-12";
				$bill->description = "Student Fee";
				$bill->state = "Unpaid";
				
				array_push($bills, $bill);
				
				$messageresponse->bills = $bills;
			}
			else if($userid == 5)
			{
				$messageresponse->status = "Success";
				$messageresponse->code = 0;
				
				$bills = Array();
				
				$bill->id = "7";
				$bill->name = "Bob Jones";
				$bill->vendor = "CSULB";
				$bill->duedate = "4-15-12";
				$bill->description = "Student Fee";
				$bill->state = "Unpaid";
				
				array_push($bills, $bill);
				
				$messageresponse->bills = $bills;
			}
			else { //Invalid userid
				$messageresponse->status = "Error";
				$messageresponse->code = 1;
			}
		}
		elseif($vendorid == "Time Warner Cable")
		{
			if($userid == 1)
			{
				$messageresponse->status = "Success";
				$messageresponse->code = 0;
				
				//Create array for bill(?)
				//$messageresponse->name = "Bob";
				//$messageresponse->vendor = "Time Warner Cable";
				//$messageresponse->duedate = "4-15-12";
				//$messageresponse->description = "Cell Phone Payment";
				//$messageresponse->state = "Unpaid";
				
				$bills = Array();
				
				$bill->id = "0";
				$bill->name = "Bob Jones";
				$bill->vendor = "Time Warner Cable";
				$bill->duedate = "4-15-12";
				$bill->description = "Cable Fee";
				$bill->state = "Unpaid";
				
				$bill2->id = "1";
				$bill2->name = "Bob Jones";
				$bill2->vendor = "Time Warner Cable";
				$bill2->duedate = "4-20-12";
				$bill2->description = "Setup Fee";
				$bill2->state = "Unpaid";
				
				$bill3->id = "2";
				$bill3->name = "Bob Jones";
				$bill3->vendor = "Time Warner Cable";
				$bill3->duedate = "4-21-12";
				$bill3->description = "HBO Fee";
				$bill3->state = "Unpaid";
				
				$bill4->id = "3";
				$bill4->name = "Bob Jones";
				$bill4->vendor = "Time Warner Cable";
				$bill4->duedate = "4-1-12";
				$bill4->description = "Piracy Fee";
				$bill4->state = "Unpaid";
				
				array_push($bills, $bill);
				array_push($bills, $bill2);
				array_push($bills, $bill3);
				array_push($bills, $bill4);
				
				$messageresponse->bills = $bills;
			}
			else if($userid == 2)
			{
				$messageresponse->status = "Success";
				$messageresponse->code = 0;
				
				$bills = Array();
				
				$bill->id = "4";
				$bill->name = "Bob Jones";
				$bill->vendor = "Time Warner Cable";
				$bill->duedate = "4-15-12";
				$bill->description = "Cable Fee";
				$bill->state = "Unpaid";
				
				array_push($bills, $bill);
				
				$messageresponse->bills = $bills;
			}
			else if($userid == 3)
			{
				$messageresponse->status = "Success";
				$messageresponse->code = 0;
				
				$bills = Array();
				
				$bill->id = "5";
				$bill->name = "Bob Jones";
				$bill->vendor = "Time Warner Cable";
				$bill->duedate = "4-15-12";
				$bill->description = "Cable Fee";
				$bill->state = "Unpaid";
				
				array_push($bills, $bill);
				
				$messageresponse->bills = $bills;
			}
			else if($userid == 4)
			{
				$messageresponse->status = "Success";
				$messageresponse->code = 0;
				
				$bills = Array();
				
				$bill->id = "6";
				$bill->name = "Bob Jones";
				$bill->vendor = "Time Warner Cable";
				$bill->duedate = "4-15-12";
				$bill->description = "Cable Fee";
				$bill->state = "Unpaid";
				
				array_push($bills, $bill);
				
				$messageresponse->bills = $bills;
			}
			else if($userid == 5)
			{
				$messageresponse->status = "Success";
				$messageresponse->code = 0;
				
				$bills = Array();
				
				$bill->id = "7";
				$bill->name = "Bob Jones";
				$bill->vendor = "Time Warner Cable";
				$bill->duedate = "4-15-12";
				$bill->description = "Cable Fee";
				$bill->state = "Unpaid";
				
				array_push($bills, $bill);
				
				$messageresponse->bills = $bills;
			}
			else { //Invalid userid
				$messageresponse->status = "Error";
				$messageresponse->code = 1;
			}
		}
		else
		{
			if($userid == 1)
			{
				$messageresponse->status = "Success";
				$messageresponse->code = 0;
				
				//Create array for bill(?)
				//$messageresponse->name = "Bob";
				//$messageresponse->vendor = "Verizon";
				//$messageresponse->duedate = "4-15-12";
				//$messageresponse->description = "Cell Phone Payment";
				//$messageresponse->state = "Unpaid";
				
				$bills = Array();
				
				$bill->id = "0";
				$bill->name = "Bob Jones";
				$bill->vendor = "Verizon";
				$bill->duedate = "4-15-12";
				$bill->description = "Cell Phone Payment";
				$bill->state = "Unpaid";
				
				$bill2->id = "1";
				$bill2->name = "Bob Jones";
				$bill2->vendor = "Verizon";
				$bill2->duedate = "4-20-12";
				$bill2->description = "Text Fee";
				$bill2->state = "Unpaid";
				
				$bill3->id = "2";
				$bill3->name = "Bob Jones";
				$bill3->vendor = "Verizon";
				$bill3->duedate = "4-21-12";
				$bill3->description = "Phone Fee";
				$bill3->state = "Unpaid";
				
				$bill4->id = "3";
				$bill4->name = "Bob Jones";
				$bill4->vendor = "Verizon";
				$bill4->duedate = "4-1-12";
				$bill4->description = "Convenence Fee";
				$bill4->state = "Unpaid";
				
				array_push($bills, $bill);
				array_push($bills, $bill2);
				array_push($bills, $bill3);
				array_push($bills, $bill4);
				
				$messageresponse->bills = $bills;
			}
			else if($userid == 2)
			{
				$messageresponse->status = "Success";
				$messageresponse->code = 0;
				
				$bills = Array();
				
				$bill->id = "4";
				$bill->name = "Bob Jones";
				$bill->vendor = "Verizon";
				$bill->duedate = "4-15-12";
				$bill->description = "Cell Phone Payment";
				$bill->state = "Unpaid";
				
				array_push($bills, $bill);
				
				$messageresponse->bills = $bills;
			}
			else if($userid == 3)
			{
				$messageresponse->status = "Success";
				$messageresponse->code = 0;
				
				$bills = Array();
				
				$bill->id = "5";
				$bill->name = "Bob Jones";
				$bill->vendor = "Verizon";
				$bill->duedate = "4-15-12";
				$bill->description = "Cell Phone Payment";
				$bill->state = "Unpaid";
				
				array_push($bills, $bill);
				
				$messageresponse->bills = $bills;
			}
			else if($userid == 4)
			{
				$messageresponse->status = "Success";
				$messageresponse->code = 0;
				
				$bills = Array();
				
				$bill->id = "6";
				$bill->name = "Bob Jones";
				$bill->vendor = "Verizon";
				$bill->duedate = "4-15-12";
				$bill->description = "Cell Phone Payment";
				$bill->state = "Unpaid";
				
				array_push($bills, $bill);
				
				$messageresponse->bills = $bills;
			}
			else if($userid == 5)
			{
				$messageresponse->status = "Success";
				$messageresponse->code = 0;
				
				$bills = Array();
				
				$bill->id = "7";
				$bill->name = "Bob Jones";
				$bill->vendor = "Verizon";
				$bill->duedate = "4-15-12";
				$bill->description = "Cell Phone Payment";
				$bill->state = "Unpaid";
				
				array_push($bills, $bill);
				
				$messageresponse->bills = $bills;
			}
			else { //Invalid userid
				$messageresponse->status = "Error";
				$messageresponse->code = 1;
			}
		}
		
		//Sending back 
		header('Content-type: application/json');
		echo json_encode($messageresponse);
	}

	public function linkToVendor()
	{
		//Expect to be sent a userid and vendor name
		$userid = $this->input->post('userid', TRUE);
		$vendorname = $this->input->post('vendorname', TRUE);
		
		$messageresponse;
		
		//Validate userid
		if($userid == 1 || $userid == 2 || $userid == 3 || $userid == 4 || $userid == 5)
		{
			$messageresponse->status = "Success";
			$messageresponse->code = 0;
		} 
		else {
			$messageresponse->status = "Error";
			$messageresponse->code = 1;
			//$messageresponse->description = "Not a valid userID";
		}
		
		//Validate vendor name
		if($vendorname == "Verizon" || $vendorname == "Best Buy" || $vendorname == "Amazon" || $vendorname == "CSULB" || $vendorname == "Time Warner Cable")
		{
			//userID already caused an error, return an error
			if($messageresponse->status == "Error")
			{
				$messageresponse->description = "Error - Not a valid userID";
			}
			else //No userID error and no vendorname error
			{
				$messageresponse->description = "Successfully linked to vendor";
			}
		}
		else 
		{
			//Invalid userid AND vendorname
			if($messageresponse->status == "Error")
			{
				$messageresponse->description = "Error - Invalid userid and vendor name";
			}
			else //Valid userid, but invalid vendorname
			{
				$messageresponse->status = "Error";
				$messageresponse->code = 1;
				$messageresponse->description = "Error - Invalid vendor name";
			}
		}
		
		//Sending back status, code, description
		header('Content-type: application/json');
		echo json_encode($messageresponse);
	}
	
	public function unlinkVendor()
	{
		//Expect to be sent a userid and vendor name
		$userid = $this->input->post('userid', TRUE);
		$vendorname = $this->input->post('vendorname', TRUE);
		
		$messageresponse;
		
		//Validate userid
		if($userid == 1 || $userid == 2 || $userid == 3 || $userid == 4 || $userid == 5)
		{
			$messageresponse->status = "Success";
			$messageresponse->code = 0;
		} 
		else {
			$messageresponse->status = "Error";
			$messageresponse->code = 1;
			//$messageresponse->description = "Not a valid userID";
		}
		
		//Validate vendor name
		if($vendorname == "CSULB" || $vendorname == "Best Buy" || $vendorname == "Amazon" || $vendorname == "CSULB" || $vendorname == "Time Warner Cable")
		{
			//userID already caused an error, return an error
			if($messageresponse->status == "Error")
			{
				$messageresponse->description = "Error - Not a valid userID";
			}
			else //No userID error and no vendorname error
			{
				$messageresponse->description = "Successfully unlinked vendor";
			}
		}
		else 
		{
			//Invalid userid AND vendorname
			if($messageresponse->status == "Error")
			{
				$messageresponse->description = "Error - Invalid userid and vendor name";
			}
			else //Valid userid, but invalid vendorname
			{
				$messageresponse->status = "Error";
				$messageresponse->code = 1;
				$messageresponse->description = "Error - Invalid vendor name";
			}
		}
		
		//Sending back status, code, description
		header('Content-type: application/json');
		echo json_encode($messageresponse);
	}
	
	//Billing Service sends message to Vendor Service that a bill has been paid
	public function billPaid() 
	{
		//Expect to be sent a billstate => 'paid'
		$billstate = $this->input->post('billstate', TRUE);
		
		if($billstate == "PAID")
		{
			$messageresponse->status = "Success";
			$messageresponse->code = 0;
		}
		else { //Error, state variable that was sent had an error
			$messageresponse->status = "Error";
			$messageresponse->code = 1;
		}
		
		//Sending back status, code
		header('Content-type: application/json');
		echo json_encode($messageresponse);
	}

}